import org.apache.spark.{SparkConf, SparkContext}
object MostPopularTeacher {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("MostPopularTeacher").setMaster("local[*]")
    val sc = new SparkContext(conf)
    val filePath = "C:\\Users\\86155\\Documents\\WeChat Files\\wxid_zvbzc6h141a722\\FileStorage\\File\\2024-10\\teacher.log"
    val data = sc.textFile(filePath)
    val nameCounts = data.map(url => {
      val parts = url.split("/")
      if (parts.length > 3) {
        val name = parts(3).split("\\?").head
        (name, 1)
      } else if (parts.length == 3) {
        val name = parts(2).split("\\?").head
        (name, 1)
      } else {
        ("", 0)
      }
    }).filter(_._1.nonEmpty)
      .reduceByKey(_ + _)
    val mostPopularTeacher = nameCounts.sortBy(_._2, ascending = false).first()
    println(s"(${mostPopularTeacher._1}, ${mostPopularTeacher._2})")
    sc.stop()
  }
}
   